package com.wiseman.runner;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.wiseman.runner.TripData.Type;

public class TripManager implements ConstValue{

	//keys
	private static final String KEY_ID = "_id";
	private static final String KEY_DATE = "_date";
	private static final String KEY_DISTANCE = "_distance";
	private static final String KEY_COSTTIME = "_costtime";
	private static final String KEY_MEMO = "_memo";
	private static final String KEY_TYPE = "_type";
	
	private static final String DB_CREATE = "CREATE  TABLE " + DB_TRIP_TABLE + " (" + KEY_ID + " INTEGER PRIMARY KEY, "
			+ KEY_DATE + " TEXT,  " + KEY_TYPE + " INTERGER, " + KEY_DISTANCE + " INTERGER, " + KEY_COSTTIME
			+ " INTERGER, " + KEY_MEMO + " TEXT)";
		
	//fields.
	private DatabaseHelper mDatabaseHelper = null;
	private SQLiteDatabase mSQLiteDatabase = null;
	private Context mContext = null;

	class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DB_NAME, null, DB_VER);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DB_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub

		}
	}



	public TripManager(Context context) {
		mContext = context;
	}

	public void open() throws SQLException {
		mDatabaseHelper = new DatabaseHelper(mContext);
		mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
	}

	public void close() {
		mDatabaseHelper.close();
	}

	public long insertData(TripData data) {
		return mSQLiteDatabase.insert(DB_TRIP_TABLE, KEY_ID, createContent(data));
	}

	public boolean deleteData(TripData data) {
		return mSQLiteDatabase.delete(DB_TRIP_TABLE, KEY_DATE  + "= ?", new String[]{data.toDateString()}) > 0;
	}
	
	public Cursor fetchAllData() {
		return mSQLiteDatabase.query(DB_TRIP_TABLE, null, null, null, null, null, null);
	}
	
	public TripData fetchData(long rowid) {

		TripData data = null;
		Cursor c = mSQLiteDatabase.query(DB_TRIP_TABLE, null, KEY_ID + "=" + rowid, null, null, null, null);

		if (c.moveToNext()) {
			data = getTripData(c);			
		}

		c.close();
		return data;
	}

	public boolean updateData(TripData data) {
		return mSQLiteDatabase.update(DB_TRIP_TABLE, createContent(data), KEY_DATE  + "= ?", new String[]{data.toDateString()}) > 0;
	}
	
	public TripData getTripData(Cursor c) {

		TripData data = null;

		if (c != null) {
			data = new TripData();

			int type = c.getInt(c.getColumnIndex(TripManager.KEY_TYPE));
			data.mType = Type.toType(type);

			String strDate = c.getString(c.getColumnIndex(TripManager.KEY_DATE));
			data.parseDate(strDate);

			int distance = c.getInt(c.getColumnIndex(TripManager.KEY_DISTANCE));
			
			data.mDistance = (float) (distance / 1000.0);
			data.mCostMinutes = c.getInt(c.getColumnIndex(TripManager.KEY_COSTTIME));
			data.mMemo = c.getString(c.getColumnIndex(TripManager.KEY_MEMO));
		}

		return data;
	}

	ContentValues createContent(TripData data) {

		ContentValues initVal = new ContentValues();

		initVal.put(KEY_DATE, data.toDateString());
		initVal.put(KEY_TYPE, data.mType.toInt());
		initVal.put(KEY_DISTANCE, data.mDistance * 1000);
		initVal.put(KEY_COSTTIME, data.mCostMinutes);
		initVal.put(KEY_MEMO, data.mMemo);

		return initVal;
	}

}
